package com.example.restaurant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.restaurant.common.RestaurantException;
import com.example.restaurant.controller.form.ModifyDishNumberByStatusForm;
import com.example.restaurant.controller.form.ModifyDishNumberForm;
import com.example.restaurant.dao.DishMapper;
import com.example.restaurant.dao.OrderMapper;
import com.example.restaurant.model.Dish;
import com.example.restaurant.model.OrderInfo;
import com.example.restaurant.dao.OrderInfoMapper;
import com.example.restaurant.model.Orders;
import com.example.restaurant.service.OrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.restaurant.service.param.AddOrderInfoParam;
import com.example.restaurant.service.param.QueryOrderInfoParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 订单详情表 服务实现类
 * </p>
 *
 * @author x
 * @since 2022-09-29
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private OrderMapper orderMapper;
    @Override
    @Transactional
    public void addOrderInfo(AddOrderInfoParam param) {
        OrderInfo orderInfo = BeanUtil.copyProperties(param, OrderInfo.class);
        int insert = orderInfoMapper.insert(orderInfo);
        if (insert<1){
            throw new RestaurantException("订单详情添加失败");
        }
    }

    @Override
    public Page<OrderInfo> queryOrderInfo(String orderNumber, Integer current, Integer size) {
        QueryWrapper<OrderInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_number",orderNumber);
        Page<OrderInfo> orderInfoPage = orderInfoMapper.selectPage(new Page<>(current,size), wrapper);
        if (orderInfoPage.getRecords().size()==0){
            throw new RestaurantException("还没有生成订单");
        }
        return orderInfoPage;
    }

    @Override
    @Transactional
    public void modifyOrderNumber(ModifyDishNumberByStatusForm form) {
        boolean flag= true;
        Dish dish = dishMapper.selectById(form.getDishId());
        QueryWrapper<OrderInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("dish_id", form.getDishId());
        List<OrderInfo> orderInfos = orderInfoMapper.selectList(wrapper);
        form.setDishImg(dish.getDishImg());
        form.setDishName(dish.getDishName());
        form.setDishPrice(dish.getDishPrice());
        OrderInfo orderInfo = BeanUtil.copyProperties(form, OrderInfo.class);
        if (form.getStatus().equals("ADD_DISH")) {
            form.setTotalNumber(dish.getStock() - form.getNumber());
            form.setQuantity(dish.getQuantity() + form.getNumber());
            orderInfo.setDishNumber(form.getNumber());
            for (OrderInfo info : orderInfos) {
                if (info.getDishId() == form.getDishId() && info.getOrderNumber().equals(form.getOrderNumber())) {
                    form.setNumber(form.getNumber() + info.getDishNumber());
                    long amount = (long) (form.getNumber()*form.getDishPrice());
                    QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
                    ordersQueryWrapper.eq("order_number",form.getOrderNumber());
                    Orders orders = orderMapper.selectOne(ordersQueryWrapper);
                    orders.setTotalAmount(orders.getTotalAmount()+amount);
                    orderMapper.modifyTotalAmount(orders.getTotalAmount(),form.getOrderNumber());
                    int i = orderInfoMapper.modifyOrderInfo(form);
                    if (i < 1) {
                        throw new RestaurantException("修改数量失败");
                    }
                    flag=false;
                }
            }
            if (flag){
                int insert = orderInfoMapper.insert(orderInfo);
                if (insert < 1) {
                    throw new RestaurantException("新增菜品失败");
                }
            }
            if (form.getQuantity()<0){
                throw new RestaurantException("库存不够");
            }
        }else {
                form.setTotalNumber(dish.getStock() + form.getNumber());
                form.setQuantity(dish.getQuantity() - form.getNumber());
                QueryWrapper<OrderInfo> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("dish_id",form.getDishId());
                wrapper1.eq("order_number",form.getOrderNumber());
                OrderInfo orderInfo1 = orderInfoMapper.selectOne(wrapper1);
                orderInfo1.setDishNumber(orderInfo1.getDishNumber()-form.getNumber());

                if (orderInfo1.getDishNumber()<0) {
                    throw new RestaurantException("你没点这么多菜");
                }
                if (orderInfo1.getDishNumber()==0) {
                    orderInfoMapper.delete(wrapper);
                } else {
                    form.setNumber(orderInfo1.getDishNumber());
                    orderInfoMapper.modifyOrderInfo(form);
                }
                long amount = (long) (orderInfo1.getDishNumber()*orderInfo1.getDishPrice());
            QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
            ordersQueryWrapper.eq("order_number",orderInfo1.getOrderNumber());
            Orders orders = orderMapper.selectOne(ordersQueryWrapper);
            orders.setTotalAmount(orders.getTotalAmount()-amount);
            orderMapper.modifyTotalAmount(orders.getTotalAmount(),orderInfo1.getOrderNumber());
        }
            ModifyDishNumberForm modifyDishNumberForm = new ModifyDishNumberForm();
            modifyDishNumberForm.setQuantity(form.getQuantity());
            modifyDishNumberForm.setDishId(form.getDishId());
            modifyDishNumberForm.setDishNumber(form.getTotalNumber());

            int i = dishMapper.modifyDishNumber(modifyDishNumberForm);
            if (i < 1) {
                throw new RestaurantException("修改数量失败");
            }

//        orderInfo.setDishNumber(form.getTotalNumber());
//        int insert = orderInfoMapper.insert(orderInfo);
//        if (insert<1){
//            throw new RestaurantException("添加加退菜数据到订单详情失败");
//        }

        }

    }

